package com.huawei.hwclouds.lubanops.demo.common.util;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.huawei.hwclouds.lubanops.demo.common.client.HeaderFactory;
import com.huawei.hwclouds.lubanops.demo.common.client.HttpClient;
import com.huawei.hwclouds.lubanops.demo.common.client.HttpClientFactory;
import com.huawei.hwclouds.lubanops.demo.common.exception.common.InternalErrorException;

public class ConfigServiceUtil {
    private static final Logger log = LoggerFactory.getLogger(ConfigServiceUtil.class);

    private static String configServerAddress;
    private static int timeout;
    private static HttpClient httpClient;

    private ConfigServiceUtil() {
    }

    public static <T extends HeaderFactory> String getConfigServerAddress(String configFileName, Class<T> factory) {
        init(configFileName, factory);
        return configServerAddress;
    }

    public static <T extends HeaderFactory> HttpClient getHttpClient(String configFileName, Class<T> factory) {
        if (null == httpClient) {
            init(configFileName, factory);
        }
        return httpClient;
    }

    private static <T extends HeaderFactory> void init(String configFileName, Class<T> factory) {
        try {
            InputStream configInput = factory.getClassLoader().getResourceAsStream(configFileName);
            if (configInput == null) {
                String errorMessage = configFileName + " not found!";
                log.error(errorMessage);
                throw new InternalErrorException(errorMessage);
            }
            Properties config = new Properties();
            config.load(configInput);
            configServerAddress = config.getProperty("client.server.address");
            if (StringUtils.isBlank(configServerAddress)) {
                String errorMessage = "client.server.address is null in " + configFileName;
                log.error(errorMessage);
                throw new InternalErrorException(errorMessage);
            }

            String sTimeout = config.getProperty("client.timeout");
            if (StringUtils.isBlank(sTimeout)) {
                String errorMessage = "client.timeout is null config-client.properties!";
                log.error(errorMessage);
                throw new InternalErrorException(errorMessage);
            }
            timeout = Integer.parseInt(sTimeout);

            String maxTotal = config.getProperty("client.maxTotal");
            int iMaxTotal = Integer.parseInt(maxTotal);

            String idle = config.getProperty("client.idle");
            int idleValue = Integer.parseInt(idle);

            httpClient = HttpClientFactory
                .getHttpClientCommon(iMaxTotal, idleValue, timeout, factory.getConstructor().newInstance());
        } catch (Exception e) {
            log.error("ConfigServiceSimple 初始化失败 {}", e.getMessage());
        }
    }

}